package com.shizhanzhe.zxbapp.task.newshare;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.task.TabAdapter;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.ErrorCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/3.
 * 转发赚钱
 */
public class ForwardFragment extends Fragment {
    @BindView(R.id.table)
    TabLayout table;
    @BindView(R.id.vp)
    ViewPager vp;
    Unbinder unbinder;
    @BindView(R.id.view1)
    View view1;
    private int cposition = 0;
    LoadService loadService;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //第一步：获取布局View
        View rootView=View.inflate(getActivity(),R.layout.fragment_forward,  null);
        unbinder = ButterKnife.bind(this, rootView);
        //第二步：注册布局View
        loadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // 重新加载逻辑
                getData();
            }
        });
        //第三步：返回LoadSir生成的LoadLayout
        return loadService.getLoadLayout();
    }

    private List<String> names ;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View gao = view.findViewById(R.id.gao);
        if (UserBean.gao>0){
            gao.setVisibility(View.GONE);
        }else {
            gao.setVisibility(View.VISIBLE);
        }
        getData();
    }
    private  void getData(){
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.SHARETYPE(), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {

                    try {
                        names = new ArrayList<>();
                        ArrayList<String> list = new ArrayList<>();
                        Gson gson = new Gson();
                        List<ShareType.DataBean> data = gson.fromJson(json, ShareType.class).getData();
                        names.add("每日推荐");
                        list.add("0");
                        for (ShareType.DataBean bean : data
                                ) {
                            names.add(bean.getCname());
                            list.add(bean.getCid()+"");
                        }
                        List<Fragment> fragments = new ArrayList<>();
                        for (int i = 0; i < names.size(); i++) {
                            fragments.add(ForWardItemFragment.newInstance(list.get(i)));
                        }
                        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), fragments, names);
                        //给ViewPager设置适配器
                        vp.setAdapter(adapter);
                        //将TabLayout和ViewPager关联起来。
                        table.setupWithViewPager(vp);
                        //设置可以滑动
                        table.setTabMode(TabLayout.MODE_SCROLLABLE);
                        table.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                            @Override
                            public void onTabSelected(TabLayout.Tab tab) {
                                cposition = tab.getPosition();
                            }

                            @Override
                            public void onTabUnselected(TabLayout.Tab tab) {

                            }

                            @Override
                            public void onTabReselected(TabLayout.Tab tab) {

                            }
                        });
                        loadService.showSuccess();
                    } catch (Exception e) {
                        loadService.showCallback(ErrorCallback.class);
                    }
                } else {
                    loadService.showCallback(ErrorCallback.class);
                }
            }
        });

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.iv_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_more:
                ForwardPopupWindow popupWindow = new ForwardPopupWindow(getActivity(), names);
                popupWindow.showDown(view1, cposition);
                popupWindow.setListener(new ForwardPopupWindow.ForwardListener() {
                    @Override
                    public void itemCheck(int itemid) {
                        table.getTabAt(itemid).select();
                    }
                });
                break;

            default:
                break;
        }
    }
}
